Total Complexity | 3 |
Total Lines | 27 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import { CommandHandler } from '@nestjs/cqrs'; |
||
8 | |||
9 | @CommandHandler(UpdateCustomerCommand) |
||
10 | export class UpdateCustomerCommandHandler { |
||
11 | constructor( |
||
12 | @Inject('ICustomerRepository') |
||
13 | private readonly customerRepository: ICustomerRepository, |
||
14 | private readonly isCustomerAlreadyExist: IsCustomerAlreadyExist |
||
15 | ) {} |
||
16 | |||
17 | public async execute(command: UpdateCustomerCommand): Promise<void> { |
||
18 | const { id, name } = command; |
||
19 | |||
20 | const customer = await this.customerRepository.findOneById(id); |
||
21 | if (!customer) { |
||
22 | throw new CustomerNotFoundException(); |
||
23 | } |
||
24 | |||
25 | if ( |
||
26 | name !== customer.getName() && |
||
27 | true === (await this.isCustomerAlreadyExist.isSatisfiedBy(name)) |
||
28 | ) { |
||
29 | throw new CustomerAlreadyExistException(); |
||
30 | } |
||
31 | |||
32 | customer.updateName(name); |
||
33 | |||
34 | await this.customerRepository.save(customer); |
||
35 | } |
||
37 |